﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AgvDemo
{
    public class MonitorShowClass
    {
        Bitmap backImage;
        public static object lockPic = new object();
        
        public MonitorShowClass()
        {
            backImage = Properties.Resources.AGV_6;
        }

        /// <summary>
        /// AGV图形界面显示处理
        /// </summary>
        /// <param name="agvInfo"></param>
        /// <param name="taskInstance"></param>      

        /// <summary>
        /// 小车图形按钮显示
        /// </summary>
        /// <param name="currentId"></param>
        /// <param name="agvNum"></param>
        /// <param name="agvImage"></param>
        /// <returns></returns>
        public void MonitorShow(PictureBox tp, int currentId, int agvNum, int lineNum, Image agvImage)
        {
            foreach (Control item in tp.Controls)
            {
                if (item is Label)
                {

                    if (item.Tag.ToString() == currentId.ToString())
                    {
                        Button btn = new Button();
                        //btn.AutoSize = true;
                        btn.Size = new Size(37, 35);
                        btn.FlatStyle = FlatStyle.Flat;
                        btn.Tag = agvNum + "-" + currentId + "-" + lineNum;
                        btn.FlatAppearance.BorderSize = 0;
                        btn.FlatAppearance.MouseDownBackColor = Color.Transparent;
                        btn.FlatAppearance.MouseOverBackColor = Color.Transparent;
                        btn.BackColor = Color.Transparent;

                        btn.BackgroundImage = agvImage;

                        btn.BackgroundImageLayout = ImageLayout.Stretch;
                        btn.Location = new Point(item.Location.X - btn.Width / 2 + item.Width / 2, item.Location.Y + item.Height - 1);
                        btn.TextAlign = ContentAlignment.TopLeft;
                        btn.Text = agvNum.ToString();//"AGV" + 
                        btn.Font = new Font("微软雅黑", 9F, FontStyle.Bold);//

                        btn.ForeColor = Color.Blue;
                        btn.BringToFront();
                        tp.Controls.Add(btn);
                    }

                }
            }
        }


        public void AgvMonitorShow(ChargeParaClass cpc, AgvFunctionClass agvF)
        {
            if (MainForm.mainForm.tpMonitor.InvokeRequired)
            {
                try
                {
                    CommunicateOkAgvShowCallBack communicateCallBack = new CommunicateOkAgvShowCallBack(AgvMonitorShow);
                    MainForm.mainForm.tpMonitor.BeginInvoke(communicateCallBack, new object[] { cpc, agvF });

                }
                catch
                {

                }

            }
            else
            {
                //lock (lockPic)
                {
                    MoreAgvShow(cpc, agvF);
                }
            }

        }

        private void MoreAgvShow(ChargeParaClass cpc, AgvFunctionClass agvF)
        {
            try
            {
                if (MainManagerClass.mapCardList.Count > 0)
                {
                    for (int i = 0; i < MainManagerClass.mapCardList.Count; i++)
                    {
                        if (cpc.CurrentStation > 0 && cpc.CurrentStation < 10)
                        {
                            if (cpc.TargetStation == 3)//仪表
                            {
                                if (MainManagerClass.mapCardList[i].card_id == cpc.CurrentStation)
                                {

                                    bool isExist = true;
                                    foreach (Control item in MainForm.mainForm.tpMonitor.Controls)
                                    {
                                        if (item is Button)
                                        {
                                            if (cpc.AgvNum.ToString() == item.Tag.ToString().Split('-')[0])
                                            {
                                                isExist = false;
                                                if (cpc.CurrentStation.ToString() == item.Tag.ToString().Split('-')[1])
                                                {
                                                    break;
                                                }
                                                else
                                                {
                                                    MainForm.mainForm.tpMonitor.Controls.Remove(item);
                                                    MonitorShow(MainForm.mainForm.tpMonitor, cpc.CurrentStation, cpc.AgvNum, cpc.TargetStation, backImage);
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    if (isExist) //只会执行一次
                                    {
                                        MonitorShow(MainForm.mainForm.tpMonitor, cpc.CurrentStation, cpc.AgvNum, cpc.TargetStation, backImage);
                                    }

                                    foreach (Control item in MainForm.mainForm.tpMonitor.Controls)
                                    {
                                        if (item is Button)
                                        {
                                            if (cpc.AgvNum.ToString() != item.Tag.ToString().Split('-')[0])
                                            {
                                                if (cpc.CurrentStation.ToString() == item.Tag.ToString().Split('-')[1])
                                                {
                                                    MainForm.mainForm.tpMonitor.Controls.Remove(item);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                int tempCurrent = cpc.CurrentStation + 20;

                                if (MainManagerClass.mapCardList[i].card_id == tempCurrent)
                                {

                                    bool isExist = true;
                                    foreach (Control item in MainForm.mainForm.tpMonitor.Controls)
                                    {
                                        if (item is Button)
                                        {
                                            if (cpc.AgvNum.ToString() == item.Tag.ToString().Split('-')[0])
                                            {
                                                isExist = false;
                                                if (tempCurrent.ToString() == item.Tag.ToString().Split('-')[1] && cpc.TargetStation.ToString() == item.Tag.ToString().Split('-')[2])
                                                {
                                                    break;
                                                }
                                                else
                                                {
                                                    MainForm.mainForm.tpMonitor.Controls.Remove(item);
                                                    MonitorShow(MainForm.mainForm.tpMonitor, tempCurrent, cpc.AgvNum, cpc.TargetStation, backImage);
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    if (isExist) //只会执行一次
                                    {
                                        MonitorShow(MainForm.mainForm.tpMonitor, tempCurrent, cpc.AgvNum, cpc.TargetStation, backImage);
                                    }

                                    foreach (Control item in MainForm.mainForm.tpMonitor.Controls)
                                    {
                                        if (item is Button)
                                        {
                                            if (cpc.AgvNum.ToString() != item.Tag.ToString().Split('-')[0])
                                            {
                                                if (tempCurrent.ToString() == item.Tag.ToString().Split('-')[1] && cpc.TargetStation.ToString() == item.Tag.ToString().Split('-')[2])
                                                {
                                                    MainForm.mainForm.tpMonitor.Controls.Remove(item);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (MainManagerClass.mapCardList[i].card_id == cpc.CurrentStation)
                            {

                                bool isExist = true;
                                foreach (Control item in MainForm.mainForm.tpMonitor.Controls)
                                {
                                    if (item is Button)
                                    {
                                        if (cpc.AgvNum.ToString() == item.Tag.ToString().Split('-')[0])
                                        {
                                            isExist = false;
                                            if (cpc.CurrentStation.ToString() == item.Tag.ToString().Split('-')[1])
                                            {
                                                break;
                                            }
                                            else
                                            {
                                                MainForm.mainForm.tpMonitor.Controls.Remove(item);
                                                MonitorShow(MainForm.mainForm.tpMonitor, cpc.CurrentStation, cpc.AgvNum, cpc.TargetStation, backImage);
                                                break;
                                            }
                                        }
                                    }
                                }

                                if (isExist) //只会执行一次
                                {
                                    MonitorShow(MainForm.mainForm.tpMonitor, cpc.CurrentStation, cpc.AgvNum, cpc.TargetStation, backImage);
                                }

                                foreach (Control item in MainForm.mainForm.tpMonitor.Controls)
                                {
                                    if (item is Button)
                                    {
                                        if (cpc.AgvNum.ToString() != item.Tag.ToString().Split('-')[0])
                                        {
                                            if (cpc.CurrentStation.ToString() == item.Tag.ToString().Split('-')[1])
                                            {
                                                MainForm.mainForm.tpMonitor.Controls.Remove(item);
                                                break;
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                       
                    }
                }
            }
            catch
            {

            }
        }


    }
}
